package com.ymatou.iapi.live.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.live.parameter.GetLiveHotProductListBean;
import com.ymatou.iapi.live.service.GetLiveHotProductListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_GetLiveHotProductList {
	
	private static GetLiveHotProductListBean getLiveHotProductListBean;
	private static GetLiveHotProductListCall getLiveHotProductListCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	private static String fileName = JsonBeanFile.CreateLiveForZyTpl;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播热门商品");
	}

	@Before
	public void caseUp() throws Exception {
		getLiveHotProductListBean = new GetLiveHotProductListBean();
		getLiveHotProductListCall = new GetLiveHotProductListCall();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getLiveHotProductListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_001() {
		 Logger.start(true,"一个推荐商品，原价，接口数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
			 	 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "77000e5a-b676-4678-a0c5-800ba0ab9fae";
			 ArrayList<String> productList = new ArrayList<String>();
			 productList.add(productId);
			 
			 //int liveId = 156429;
			 Thread.sleep(5000);
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 SellerProductCallService.addProductsIntoTopList(liveId, productList);
			 
			 Thread.sleep(2000);
			 
			 getLiveHotProductListBean.setLiveId(liveId);
			 getLiveHotProductListBean.setUserId(4085);
			 
			 getLiveHotProductListCall.setData(getLiveHotProductListBean);
			 getLiveHotProductListCall.callService();
			 
			 Logger.comment("getLiveInfoCall send:" + getLiveHotProductListCall.getEntityString());
			 Logger.comment("getLiveInfoCall response:" + getLiveHotProductListCall.getReturnData());
			 
			 this.normalVerifyData(getLiveHotProductListCall, liveId, 0);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_002() {
		 Logger.start(true,"两个推荐商品，原价，接口数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 String productId1 = SellerProductCallService.addNewProductOnSale(sellerId, 
			 	 sellerName, null).getJSONObject("Data").getString("ProductID");
			 String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, 
				 	 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "77000e5a-b676-4678-a0c5-800ba0ab9fae";
			 ArrayList<String> productList = new ArrayList<String>();
			 productList.add(productId1);
			 productList.add(productId2);
			 
			 //int liveId = 156429;
			 Thread.sleep(5000);
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId1);
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId2);
			 SellerProductCallService.addProductsIntoTopList(liveId, productList);
			
			 getLiveHotProductListBean.setLiveId(liveId);
			 getLiveHotProductListBean.setUserId(4085);
			 
			 getLiveHotProductListCall.setData(getLiveHotProductListBean);
			 getLiveHotProductListCall.callService();
			 
			 Logger.comment("getLiveInfoCall send:" + getLiveHotProductListCall.getEntityString());
			 Logger.comment("getLiveInfoCall response:" + getLiveHotProductListCall.getReturnData());
			 
			 this.normalVerifyData(getLiveHotProductListCall, liveId, 0);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_003() {
		 Logger.start(true,"一个推荐商品，新客价，接口数据与mongdb验证");
		 try {
			 Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			 
			 int userId = (Integer)user.get("userId");
			 
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
			 	 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "77000e5a-b676-4678-a0c5-800ba0ab9fae";
			 ArrayList<String> productList = new ArrayList<String>();
			 productList.add(productId);
			 
			 //int liveId = 156429;
			 Thread.sleep(5000);
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 SellerProductCallService.addProductsIntoTopList(liveId, productList);
			
			 getLiveHotProductListBean.setLiveId(liveId);
			 getLiveHotProductListBean.setUserId(userId);
			 
			 getLiveHotProductListCall.setData(getLiveHotProductListBean);
			 getLiveHotProductListCall.callService();
			 
			 Logger.comment("getLiveInfoCall send:" + getLiveHotProductListCall.getEntityString());
			 Logger.comment("getLiveInfoCall response:" + getLiveHotProductListCall.getReturnData());
			 
			 this.normalVerifyData(getLiveHotProductListCall, liveId, 1);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_004() {
		 Logger.start(true,"一个推荐商品，VIP价，接口数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
			 	 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "77000e5a-b676-4678-a0c5-800ba0ab9fae";
			 ArrayList<String> productList = new ArrayList<String>();
			 productList.add(productId);
			 
			 //int liveId = 156429;
			 Thread.sleep(5000);
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 SellerProductCallService.addProductsIntoTopList(liveId, productList);
			
			 getLiveHotProductListBean.setLiveId(liveId);
			 getLiveHotProductListBean.setUserId(20226821);
			 
			 getLiveHotProductListCall.setData(getLiveHotProductListBean);
			 getLiveHotProductListCall.callService();
			 
			 Logger.comment("getLiveInfoCall send:" + getLiveHotProductListCall.getEntityString());
			 Logger.comment("getLiveInfoCall response:" + getLiveHotProductListCall.getReturnData());
			 
			 this.normalVerifyData(getLiveHotProductListCall, liveId, 2);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	public void normalVerifyData(GetLiveHotProductListCall getLiveHotProductListCall, int liveId, int priceType) throws JSONException, Exception {
		LiveModelWapper wapper = new LiveModelWapper();
		ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
		 
		Logger.verifyEquals(200, getLiveHotProductListCall.getStatus(), "判断接口status是否为200");
		Logger.verifyNotNull(getLiveHotProductListCall.getResult(), "验证result是否为空");
		Logger.verifyEquals(wapper.getActivityId(liveId).get("ActivityId"), getLiveHotProductListCall.getLive().getInt("id"), 
				"验证直播id是否与mongodb一致");
		Logger.verifyEquals(wapper.getActivityId(liveId).get("ActivityContent"), getLiveHotProductListCall.getLive().getString("description"), 
				"验证直播描述是否与mongodb一致");
		wapper.getActivityId(liveId).get("EndTime");
		Logger.verifyEquals(YMTDateUtil.CST2TimeStamp(((Date) wapper.getActivityId(liveId).get("EndTime")).toString()), 
				getLiveHotProductListCall.getLive().getLong("expireTime"), "验证直播过期时间是否与mongodb一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ActivityName")), 
				getLiveHotProductListCall.getLive().getString("name"),"验证直播名称是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ShopAddress")), 
				getLiveHotProductListCall.getLive().getString("shopAddress"),"验证直播地址是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ActivityContent")), 
				getLiveHotProductListCall.getLive().getString("description"),"验证买手信息中直播简介是否与mongo一致");
		
		for (int i = 0; i < getLiveHotProductListCall.getProdList().length(); i++) {
			String productId = getLiveHotProductListCall.getProdList().getJSONObject(i).getString("id");
			Logger.verifyEquals(( productDetailModelWapper.getProductId(productId).get("ProductId")), 
					productId,"验证推荐的商品id是否与mongo一致");
			JSONArray picList = new JSONArray(productDetailModelWapper.getProductId(productId).get("PicList").toString());
			Logger.verifyEquals(this.changeProductImageSize(picList.get(0).toString()), 
					getLiveHotProductListCall.getProdList().getJSONObject(i).getString("pic"),"验证推荐的商品图片是否与mongo一致");
			JSONObject priceObject = getLiveHotProductListCall.getProdList().getJSONObject(i).getJSONObject("price");
			Logger.verifyEquals(priceType, priceObject.getInt("type"),"验证返回的价格类型是否与期望一致");
		}
	}
	
	public String changeProductImageSize(String originUrl) {
		String changeUrl = "";
		String subString = originUrl.substring(0, originUrl.length() - 4);
		
		if (originUrl.contains("shangou") && subString.endsWith("_n_w_o")) {
			changeUrl = subString.substring(0, subString.length() - 1) + "lb.jpg";
		} else {
			changeUrl = originUrl;
		}
		
		return changeUrl;
	}
	
	public static void main(String[] args) {
		Ts_GetLiveHotProductList test = new Ts_GetLiveHotProductList();
		String originUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2A/17/rBBlD1eqnq-AEvUHAANhqAY9_CQ839_n_w_o.jpg";
		System.out.println(test.changeProductImageSize(originUrl));
	}
}
